let db = require("../db/db")

let handle = async function (action, data, login_info, req, res) {
	switch (action) {
		case "load": //根据当前登录用户权限load
			let user_id = login_info.user_id;
			let c_sql = "select * from sys_menu where is_deleted=0 and page_id \
						in (select page_id from sys_function where is_deleted=0 and name='访问' and id \
							in (select func_id from sys_role_func_r where is_deleted=0 and role_id \
								in (select role_id from sys_user_role_r where is_deleted=0 and user_id=? \
						)))";
			let p_sql = "select * from sys_menu where id in (select parent_id from (" + c_sql + ") ct)";
			let pp_sql = "select * from sys_menu where id in (select parent_id from (" + p_sql + ") pt)";
			let sql = c_sql + " union " + p_sql + " union " + pp_sql;
			let result = await db.query(sql, [user_id, user_id, user_id]);
			return result;
			break;
		default:
			return Error('未知的action:' + action);
	}
}

module.exports = { handle };